/*
 * @Description: count的容器组件
 * @Author: 姚泽毅
 * @Date: 2021-09-01 08:59:37
 * @LastEditTime: 2021-09-02 15:52:32
 */
import { connect } from 'react-redux'
import { increment, decrement, incrementAsync } from '../../redux/actions/count'
import React, { Component } from 'react'

class Count extends Component {
    increment = () => {
        const { value } = this.selectNumber
        this.props.increment(value * 1)
    }
    decrement = () => {
        const { value } = this.selectNumber
        this.props.decrement(value * 1)
    }
    incrementIfOdd = () => {
        const { value } = this.selectNumber
        if (this.props.count % 2 !== 0) {
            this.props.increment(value * 1)
        }
    }
    incrementAsync = () => {
        const { value } = this.selectNumber
        this.props.incrementAsync(value * 1, 500)
    }
    render() {
        return (
            <div>
                <h1>当前求和为：{this.props.count},下方组件总人数为：{this.props.person.length}</h1>
                <select ref={c => this.selectNumber = c}>
                    <option value="1">1</option>
                    <option value="2">2</option>
                    <option value="3">3</option>
                </select>&nbsp;&nbsp;
                <button onClick={this.increment}>+</button>&nbsp;&nbsp;
                <button onClick={this.decrement}>-</button>&nbsp;&nbsp;
                <button onClick={this.incrementIfOdd}>当前求和为奇数再加</button>&nbsp;&nbsp;
                <button onClick={this.incrementAsync}>异步加</button>
            </div>
        )
    }
}

export default connect(
    state => ({
        count: state.count,
        person: state.person
    }),
    {
        increment,
        decrement,
        incrementAsync
    }
)(Count)
